package com.utils;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import androidx.annotation.IntegerRes;
import androidx.core.content.FileProvider;

import java.io.File;
import java.util.Locale;

/**
 * @describe: 路径工具类
 * @Author: lixiaopeng
 * @Date: 2020/11/11
 */
public final class PathUtils {

    public static File getCacheDir() {
        return Utils.getApp().getExternalCacheDir();
    }

    public static String randomCacheImage() {
        return getCacheDir() + File.separator + "IMG_" + System.currentTimeMillis() + ".jpg";
    }

    /**
     * 本地资源转URI
     *
     * @param resources
     * @param fileRes   such as R.drawable.xxx,R.mipmap.xxx...
     * @return
     */
    public static Uri getResourceUri(Resources resources, @IntegerRes int fileRes) {
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + resources.getResourcePackageName(fileRes) + "/"
                + resources.getResourceTypeName(fileRes) + "/"
                + resources.getResourceEntryName(fileRes));
    }

    /**
     * file转换为uri
     *
     * @param file
     * @return
     */
    public static Uri fileToUri(File file) {
        return FileProvider.getUriForFile(Utils.getApp(), Utils.getApp().getPackageName() + ".util.provider", file);
    }

    /**
     * 图片选择器选中图片后返回的id，通过此方法返回对应图片的uri
     */
    public static Uri imageId2Uri(int id) {
        return ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
    }

    /**
     * 图片选择器选中视频后返回的id，通过此方法返回对应图片的uri
     */
    public static Uri videoId2Uri(int id) {
        return ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
    }
}
